package com.ksyun.alert.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @author Zhang
 * @date 2024-05-05
 */
public class TimestampConverter {

    public static String intToDateTimeString(long timestamp) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(java.time.Instant.ofEpochSecond(timestamp), java.time.ZoneId.of("Asia/Shanghai"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return localDateTime.format(formatter);
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss格式的字符串转换为秒级时间戳
     * @param datetimeString 字符串时间
     */
    public static int stringToTimestamp(String datetimeString) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetimeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return (int) localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
    }

    /**
     * LocalDateTime砖时间戳
     * @param dateTime 时间
     * @return 时间戳
     */
    public static int localDateTimeToInt(LocalDateTime dateTime) {
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        java.time.ZonedDateTime zonedDateTime = dateTime.atZone(zoneId);
        return (int) zonedDateTime.toEpochSecond();
    }
}
